﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<div id="createDialog" class="crudDialog">
	<form id="createForm" method="post">
		<div class="form-group">
			<label for="code">编码</label>
			<input id="code" type="text" class="form-control" name="code" maxlength="25">
		</div>
		<div class="form-group">
			<label for="name">名称</label>
			<input id="name" type="text" class="form-control" name="name" maxlength="50">
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label" for="isinput">
				是否允许输入：</label>
			<div class="col-sm-5">
				<select id="isinput" name="isinput" class="form-control">
					<option value="1">是</option>
					<option value="0">否</option>
				</select>
			</div>
		</div>
		<div class="form-group">
			<label for="description">描述</label>
			<input id="description" type="text" class="form-control" name="description" maxlength="250">
		</div>
		<div class="form-group text-right dialog-buttons">
			<a class="waves-effect waves-button" href="javascript:;" onclick="createSubmit();">保存</a>
			<a class="waves-effect waves-button" href="javascript:;" onclick="createDialog.close();">取消</a>
		</div>
	</form>
</div>
<script>

    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                code: {
                    required: true,
                    maxlength: 25
                },
                name: {
                    required: true,
                    maxlength: 50
                },
                description: {
                    required: true,
                    maxlength: 250
                }
            },
            messages: {
                code:{
                    required: "编码是必填项。",
                    maxlength: "编码超出25个字符。"
                },
                name:{
                    required: "名称是必填项。",
                    maxlength: "名称超出50个字符。"
                },
                description: {
                    maxlength: "描述长度超出2550个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

	function createSubmit() {
        if(validateForm().form()){
			$.ajax({dataType: 'json',
				type: 'post',
				url: '${basePath}/manage/specialconfig/create',
				data: $('#createForm').serialize(),
				success: function(result) {
					if (result.code != 1) {
						if (result.data instanceof Array) {
							$.each(result.data, function(index, value) {
								$.confirm({
									theme: 'dark',
									animation: 'rotateX',
									closeAnimation: 'rotateX',
									title: false,
									content: value.errorMsg,
									buttons: {
										confirm: {
											text: '确认',
											btnClass: 'waves-effect waves-button waves-light'
										}
									}
								});
							});
						} else {
								$.confirm({
									theme: 'dark',
									animation: 'rotateX',
									closeAnimation: 'rotateX',
									title: false,
									content: result.data,
									buttons: {
										confirm: {
											text: '确认',
											btnClass: 'waves-effect waves-button waves-light'
										}
									}
								});
						}
					} else {
						createDialog.close();
						$table.bootstrapTable('refresh');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$.confirm({
						theme: 'dark',
						animation: 'rotateX',
						closeAnimation: 'rotateX',
						title: false,
						content: textStatus,
						buttons: {
							confirm: {
								text: '确认',
								btnClass: 'waves-effect waves-button waves-light'
							}
						}
					});
				}
			});
		}
	}
</script>